import random
import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from pykeyboard import PyKeyboard
import os

class keytest():
    def __init__(self):
        os.system("taskkill /F /IM firefox.exe")
    def keyT(self):

        dr=webdriver.Firefox()
        dr.get("http://www.baidu.com")
        # dr.find_element_by_xpath('//*[@id="kw"]').send_keys("webdriver")
        dr.find_element_by_css_selector('#kw').send_keys("webdriver")
        time.sleep(2)
        dr.find_element_by_class_name("s_ipt").send_keys(Keys.BACK_SPACE)
        time.sleep(2)
        dr.find_element_by_id("kw").send_keys(Keys.CONTROL,'a')
        time.sleep(2)

        dr.find_element_by_class_name("s_ipt").send_keys(Keys.DELETE)
    def mou(self):
        dr = webdriver.Firefox()
        dr.get("http://localhost:80/agileone")
        dr.find_element_by_id("username").send_keys("admin")
        dr.find_element_by_id("password").send_keys("admin")
        # dr.find_element_by_id("login").click()
        time.sleep(2)
        #公告管理右键点击
        ActionChains(dr).context_click(dr.find_element_by_link_text('※ 公告管理 ※')).perform()
        # time.sleep(2)
        # keyboard=PyKeyboard()
        # keyboard.press_key(keyboard.down_key)#按下键
        # keyboard.release_key(keyboard.down_key)#释放下键
        # time.sleep(2)
        # keyboard.press_key(keyboard.enter_key)#按回车
        # keyboard.release_key(keyboard.enter_key)#释放回车
        # time.sleep(2)

    def sw(self):
        dr = webdriver.Chrome()
        dr.get("http://localhost:80/agileone")
        dr.find_element_by_id("username").send_keys("admin")
        dr.find_element_by_id("password").send_keys("admin")
        dr.find_element_by_id("login").click()
        time.sleep(2)
        ActionChains(dr).context_click(dr.find_element_by_link_text('※ 公告管理 ※')).perform()
        Ke= PyKeyboard()
        Ke.press_key(Ke.down_key)#按下键

        time.sleep(2)
        Ke.press_key(Ke.down_key)
        # 按下键
        time.sleep(2)
        Ke.press_key(Ke.enter_key)#按回车
        print(dr.window_handles)#打印所有窗口的句柄
        print(dr.current_window_handle)#打印当前窗口的句柄
        time.sleep(2)
        for handle in dr.window_handles:
            if handle != dr.current_window_handle:#遍历窗口句柄，如果遍历到的句柄不是当前窗口，则切换新的窗口
                time.sleep(2)
                dr.switch_to.window(handle)
        time.sleep(2)
        print(dr.current_window_handle)#打印当前窗口的句柄，已经切换后的窗口

    def upload(self):
        dr = webdriver.Firefox()  # 相对路径方式调用驱动
        dr.get("http://192.168.2.184:80/woniusales/")#首页
        dr.find_element_by_id('username').send_keys('admin')#用户名
        dr.find_element_by_id('password').send_keys('admin123')#密码
        dr.find_element_by_id("verifycode").send_keys('0000')#验证码
        time.sleep(2)
        # dr.find_element_by_xpath('(//button[@type="button"])[5]').click()#点击登录
        k=PyKeyboard()
        k.press_key(k.enter_key)#点击登录
        time.sleep(5)
        dr.find_element_by_link_text("批次管理").click()
        time.sleep(2)
        num=random.randint(1,999)
        dr.find_element_by_id("batchname").clear()
        time.sleep(2)
        dr.find_element_by_id("batchname").send_keys("GB20181214-%d"%num)

        #webdriver直接send_keys()，中英文路径均可实现
        # dr.find_element_by_id("batchfile").send_keys(r"E:\FTP\02-第二阶段-专业核心课程\02-系统测试项目\02-教学软件\销售出库单-20171020-Test.xls")
        # time.sleep(2)
        # dr.find_element_by_css_selector("input.btn-primary.form-control").click()


        #鼠标点击“选择文件”
        ActionChains(dr).click(dr.find_element_by_id('batchfile')).perform()
        time.sleep(2)
        k.type_string(r"E:\FTP\sales-20171020-Test.xls")#只支持英文路径
        time.sleep(2)
        k.press_key(k.enter_key)
        time.sleep(2)
        dr.find_element_by_css_selector("input.btn-primary.form-control").click()
        time.sleep(2)
        # string=str(time.localtime())
        random_num=random.randint(10000,99999)
        # dr.save_screenshot(r"E:\FTP\test%d.png"%random_num)#截图
        dr.get_screenshot_as_file(r"E:\FTP\test%d.png"%random_num)#截图






if __name__ == '__main__':
    k=keytest()
    # k.keyT()
    # k.mou()
    k.sw()
    # k.upload()